package com.adcc.msgsimulator.util;

import com.adcc.msgsimulator.bo.Msg620BO;
import com.adcc.msgsimulator.dto.Msg620Converter;
import com.adcc.msgsimulator.entity.Msg620DO;
import com.adcc.msgsimulator.entity.SvcDO;
import com.adcc.msgsimulator.service.Msg620Service;
import com.adcc.msgsimulator.service.SvcService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.common.base.Strings;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Map;


/**
 * RocketMQ 工厂
 */
@Slf4j
@Component
@RocketMQMessageListener(topic = "${rocketmq.topic}", consumerGroup = "${rocketmq.group}")
public class RocketMQFactory implements RocketMQListener<String> {

    @Value("${rocketmq.nameServer}")
    private String namesrvAddr;

    @Value("${rocketmq.topic}")
    private String consumerTopic;

    @Autowired
    private Msg620Service msg620Service;

    @Autowired
    private SvcService svcService;

    DefaultMQPushConsumer consumer;

    @Resource
    private Msg620Converter msg620Converter;

    @Override
    public void onMessage(String message) {
        try {
            System.out.println("消费者收到信息：===" + message);
            //判断msg618还是svc
            JSONObject jsonObject = JSON.parseObject(message);
            JSONObject meta = (JSONObject) jsonObject.get("Meta");
            char id = meta.get("ID").toString().charAt(0);
            if(id=='B'){
                Msg620DO msg620DO = this.toMsg620D0(message);
                msg620Service.save(msg620DO);

            }else if (id=='S'){
                //保存svc
                SvcDO svcDO = this.toSvcDO(message);
                svcService.save(svcDO);
            }
        } catch (Exception ex) {
            log.error("onMessage() error", ex);
        }
    }
    public Msg620DO  toMsg620D0(String message){
        JSONObject jsonObject = JSON.parseObject(message);
        //处理数据
        JSONObject data = (JSONObject) jsonObject.get("Data");
        JSONObject msgJson= (JSONObject)data.get("Msg");
        Msg620DO msg = JSON.parseObject(data.get("Msg").toString(), Msg620DO.class);
        JSONObject meta = (JSONObject) jsonObject.get("Meta");
        msg.setMsgID(meta.get("ID").toString());
        msg.setMsg(data.get("RawMsg").toString());
        msg.setMsgLength(msg.getMsg().length());
        msg.setMsgType(Integer.parseInt(meta.get("MsgType").toString()));
        msg.setRecvTime(meta.get("TimeStamp").toString());
        msg.setTail(msgJson.get("AN").toString());
        msg.setFlightNum(msgJson.get("FI").toString());
        msg.setAgency(msg.getFlightNum().substring(0,2));
        //todo
        msg.setRgsTime(null);
        return msg;
    }

    public SvcDO  toSvcDO(String message){
        SvcDO svcDo=new SvcDO();
        JSONObject jsonObject = JSON.parseObject(message);
        JSONObject meta = (JSONObject) jsonObject.get("Meta");
        JSONObject data = (JSONObject) jsonObject.get("Data");
        svcDo.setMsgID(meta.get("ID").toString());
        svcDo.setMsg(data.get("RawMsg").toString());
        svcDo.setMsgLength(svcDo.getMsg().length());
        svcDo.setRecvTime(meta.get("TimeStamp").toString());
        return  svcDo;
    }
}
